package com.example.minefieldkeepercloud.view.pop;

import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.PopupWindow;

import com.example.minefieldkeepercloud.R;
import com.example.minefieldkeepercloud.adapter.MachineMaintainPopAdapter;

import java.util.ArrayList;
import java.util.List;

/**
 * 维修维护选择
 */
public class RepairSelectPop extends PopupWindow implements View.OnClickListener {

    private Activity context;
    private View view;
    private OnSelectListener onSelectListener;
    private MachineMaintainPopAdapter machineMaintainPopAdapter;

    public RepairSelectPop(Context context, OnSelectListener onSelectListener) {
        super(context);
        this.context = (Activity) context;
        this.onSelectListener = onSelectListener;
        //this.dataBeanList = dataBeanList;
        initPop();
        initView();


    }


    private void initView() {
        View v = view.findViewById(R.id.view_null);
        v.setOnClickListener(this);

        ListView lv_houselayout = view.findViewById(R.id.lv_houselayout);
        final List<String> list = new ArrayList<>();
        list.add("全部");
        list.add("维修");
        list.add("维护");
        list.add("返修");
        machineMaintainPopAdapter = new MachineMaintainPopAdapter(context, list);
        lv_houselayout.setAdapter(machineMaintainPopAdapter);

        lv_houselayout.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                onSelectListener.selectContent(list.get(i));
                machineMaintainPopAdapter.setSelected(i);

                if (i == 0) {
                    onSelectListener.selectnumber(0+ "");
                } else if (i == 1) {
                    onSelectListener.selectnumber(3 + "");
                } else if (i == 2) {
                    onSelectListener.selectnumber(6+ "");
                }else if (i == 3) {
                    onSelectListener.selectnumber(7+ "");
                }
                dismiss();
            }
        });
    }


    /**
     * 初始化pop样式
     */
    private void initPop() {

        view = LayoutInflater.from(context).inflate(R.layout.pop_machinemaintain, null);
        this.setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
        this.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
        this.setContentView(view);


        ColorDrawable dw = new ColorDrawable(context.getResources().getColor(R.color.gray_bg));
        //设置SelectPicPopupWindow弹出窗体的背景
        setBackgroundDrawable(dw);

        //获取popwindow焦点
        setFocusable(true);
        //设置popwindow如果点击外面区域，便关闭。
        setOutsideTouchable(true);
    }

    /**
     * \
     * 设置透明背景
     *
     * @param bgAlpha
     */
    public void backgroundAlpha(float bgAlpha) {
        WindowManager.LayoutParams lp = context.getWindow().getAttributes();
        lp.alpha = bgAlpha; //0.0-1.0
        context.getWindow().setAttributes(lp);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {

            case R.id.view_null:
                //点击半透明
                dismiss();
                break;

        }
    }

    /**
     * 选择回调
     */
    public interface OnSelectListener {
        public void selectContent(String str);

        public void selectnumber(String str);
    }
}
